package cn.zjyj.module.zjyj.controller.admin.risksample;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.zjyj.framework.common.common.pojo.PageParam;
import cn.zjyj.framework.common.common.pojo.PageResult;
import cn.zjyj.framework.common.common.pojo.CommonResult;
import cn.zjyj.framework.common.common.util.object.BeanUtils;
import static cn.zjyj.framework.common.common.pojo.CommonResult.success;

import cn.zjyj.framework.excel.core.util.ExcelUtils;

import cn.zjyj.framework.apilog.core.annotation.ApiAccessLog;
import static cn.zjyj.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.zjyj.module.zjyj.controller.admin.risksample.vo.*;
import cn.zjyj.module.zjyj.dal.dataobject.risksample.RiskSampleDO;
import cn.zjyj.module.zjyj.service.risksample.RiskSampleService;

@Tag(name = "管理后台 - 风险样本")
@RestController
@RequestMapping("/zjyj/risk-sample")
@Validated
public class RiskSampleController {

    @Resource
    private RiskSampleService riskSampleService;

    @PostMapping("/create")
    @Operation(summary = "创建风险样本")
    @PreAuthorize("@ss.hasPermission('zjyj:risk-sample:create')")
    public CommonResult<Long> createRiskSample(@Valid @RequestBody RiskSampleSaveReqVO createReqVO) {
        return success(riskSampleService.createRiskSample(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新风险样本")
    @PreAuthorize("@ss.hasPermission('zjyj:risk-sample:update')")
    public CommonResult<Boolean> updateRiskSample(@Valid @RequestBody RiskSampleSaveReqVO updateReqVO) {
        riskSampleService.updateRiskSample(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除风险样本")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('zjyj:risk-sample:delete')")
    public CommonResult<Boolean> deleteRiskSample(@RequestParam("id") Long id) {
        riskSampleService.deleteRiskSample(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除风险样本")
                @PreAuthorize("@ss.hasPermission('zjyj:risk-sample:delete')")
    public CommonResult<Boolean> deleteRiskSampleList(@RequestParam("ids") List<Long> ids) {
        riskSampleService.deleteRiskSampleListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得风险样本")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('zjyj:risk-sample:query')")
    public CommonResult<RiskSampleRespVO> getRiskSample(@RequestParam("id") Long id) {
        RiskSampleDO riskSample = riskSampleService.getRiskSample(id);
        return success(BeanUtils.toBean(riskSample, RiskSampleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得风险样本分页")
    @PreAuthorize("@ss.hasPermission('zjyj:risk-sample:query')")
    public CommonResult<PageResult<RiskSampleRespVO>> getRiskSamplePage(@Valid RiskSamplePageReqVO pageReqVO) {
        PageResult<RiskSampleDO> pageResult = riskSampleService.getRiskSamplePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RiskSampleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出风险样本 Excel")
    @PreAuthorize("@ss.hasPermission('zjyj:risk-sample:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRiskSampleExcel(@Valid RiskSamplePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RiskSampleDO> list = riskSampleService.getRiskSamplePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "风险样本.xls", "数据", RiskSampleRespVO.class,
                        BeanUtils.toBean(list, RiskSampleRespVO.class));
    }

}